package com.lyf.core.ui.web;

import android.graphics.Bitmap;
import android.webkit.WebView;

import com.just.agentweb.WebViewClient;
import com.lyf.core.ui.activity.BaseWebViewActivity;

import java.lang.ref.WeakReference;

/**
 * WebView加载rul过程的控制客户端
 */
public class CommonWebViewClient extends WebViewClient {
    private WeakReference<BaseWebViewActivity> reference;

    public CommonWebViewClient(BaseWebViewActivity activity) {
        this.reference = new WeakReference<>(activity);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (reference == null || reference.get() == null || reference.get().isFinishing() || reference.get().clientListnenr==null) {
            return false;
        }

        return reference.get().clientListnenr.shouldOverrideUrlLoading(view, url);
    }

    /**
     * 开始
     */
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

        if (reference == null || reference.get() == null || reference.get().isFinishing() || reference.get().clientListnenr==null) {
            return;
        }
        reference.get().showLoading();
        reference.get().clientListnenr.onPageStarted(view, url, favicon);
    }

    /**
     * 结束
     */
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (reference == null || reference.get() == null || reference.get().isFinishing() || reference.get().clientListnenr==null) {
            return;
        }
        view.requestFocus();
        if (isWebViewTitleFindErrorPager(view)) {
            reference.get().clientListnenr.onPageFailed(view, url);
        } else {
            reference.get().clientListnenr.onPageSuccess(view, url);
        }
        reference.get().clientListnenr.onPageFinished(view, url);

        reference.get().stopLoading();
    }

    /**
     * 失败
     */
    @Override
    public void onReceivedError(final WebView view, int errorCode,
                                String description, final String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
//        view.postDelayed(() -> {
//        }, 10);
        if (reference == null || reference.get() == null || reference.get().isFinishing() || reference.get().clientListnenr==null) {
            return;
        }
        reference.get().clientListnenr.onPageFailed(view, failingUrl);
        reference.get().stopLoading();
    }

    private boolean isWebViewTitleFindErrorPager(WebView view) {
        if (reference == null || reference.get() == null || reference.get().isFinishing() || reference.get().clientListnenr==null) {
            return false;
        }

        if ("找不到网页".equals(view.getTitle()) || "Webpage not available".equals(view.getTitle())) {
            return true;
        }
        return false;
    }

    public void clean(){
        if (reference!=null){
            reference.clear();
            reference=null;
        }
    }
}
